package guiCode;

import internalMail.createMailBox;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JTable;

import com.sun.rowset.CachedRowSetImpl;
import basic.ClientConsole;
import flexibility.CachedRowSetTableModel;
import flexibility.userData;
import gui.secretaryScreen;
import secretary.*;
import secretaryGUI.createSecretaryWindow;
import secretaryGUI.updateLowerTableEntries;
import secretaryGUI.updateUpperTableEntries;

public class secretaryScreenCode {
	
	
	private userData secretaryData = new userData();
	private ClientConsole secretaryConnection;
	private secretaryScreen ss ;
	private static Integer myID;
	private int studentID;
	private double teacherID;
	private CachedRowSetImpl crs;
	private updateUpperTableEntries UUTE = new updateUpperTableEntries();
	private updateLowerTableEntries ULTE = new updateLowerTableEntries();
	checkClassRegex ccr = new checkClassRegex();
	setSemesterEditable sse = new setSemesterEditable();
	private int editMode = 0;
	private String assigningErrorLogName;
	private int sourceTable;
	private int destinationTable;
	private secretaryScreenCode ssc;
	private defineSemester ds = new defineSemester();
	
	public void backToDefault(createSecretaryWindow csw, secretaryScreenCode code){
		ssc = code;
		//this will clear the entries from the tables
		secretaryData.deleteAll();
		secretaryData.addData(89);
		secretaryData.addData(1);
		secretaryConnection.acceptMsg(secretaryData);
		crs = secretaryConnection.getResults();
		UUTE.update(csw, crs);
		ULTE.update(csw, crs);
	}
	
	public secretaryScreenCode(){
		//this is an empty constructor
		//we need this in order to create an instance of this class
		//so we will have approach to its close method
	}
	
	public  secretaryScreenCode(ClientConsole startConnection,int id){
		//this is the constructor that creates the instance 
		//and initiates all its connections
		setConnection(startConnection);
		setMyID(id);
		
	}
	
	public void setConnection(ClientConsole conn) {
		secretaryConnection = conn;
		try {
			crs = new CachedRowSetImpl();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setMyID(int id) {
		myID = id;
		//ss = new secretaryScreen();
	}
	
	public void setSecretaryGUI(secretaryScreen gui){
		//this method will create the secretary gui reference
		ss = gui;
	}

	public boolean isOpenSemester(int semester) {
		checkOpenSemester cos = new checkOpenSemester();
		return cos.isOpenSemester(secretaryConnection, crs, secretaryData, semester);
	}

	public void openNewSemester(String date, int semester) {
		openNewSemester ons = new openNewSemester();
		ons.openNewSemester(date, secretaryConnection, crs, secretaryData, semester);
	}

	public CachedRowSetTableModel getClasses(int semesterCode) throws SQLException {
		getClasses gc = new getClasses();
		return new CachedRowSetTableModel(gc.getClasses(secretaryConnection, crs, secretaryData, semesterCode));
	}
	

	public void createNewClass(createSecretaryWindow csw, secretaryScreenCode ssc,JFrame secretaryScreen,String className, int semester) {
		createNewClass cnc = new createNewClass();
		cnc.createNewClass(csw, ssc, secretaryScreen,className, secretaryConnection, crs, secretaryData, ss, semester);
	}

	public void closeSecretearyScreen() {// closing the connection to the
											// database and exits application
		closeSecretaryScreen css = new closeSecretaryScreen();
		css.closeSecretearyScreen(myID, secretaryConnection, secretaryData);
	}
	
	public void setSemesterForEditing(int semester){
		//this method will set the semester for editing, 0 is for current semester,
		//1 is for next semester
		sse.setSemesterToEdit(semester);
	}
	
	public int getSemesterForEdit(){
		return sse.getSemesterToEdit();
	}
	
	public void changeClassName(createSecretaryWindow csw, secretaryScreenCode ssc,JFrame secretaryScreen,int code, int type, String change, String id, String semester){
		changeClassName ccn = new changeClassName();
		ccn.setChanges(csw, ssc,secretaryScreen, code,type, change, id,semester, secretaryConnection);
	}
	
	public boolean checkForCurrentSemester( ){
		//this will check if there is a current open semester when user logs on 
		checkForCurrentSemester cfcs = new checkForCurrentSemester();
		return cfcs.checkForCurrentSemester(secretaryConnection, crs, secretaryData);
	}
	
	public void setEditMode(int mode){
		//mode = 1 --> upper table is in the class view (upper table show the relevant classes)
		//mode = 2 --> upper table is in specific class view(class was chosen and its student are in the upper table)
		//mode = 3 --> system in edit mode, before adding/removing students
		//mode = 4 --> system is in add students to class mode
		//mode = 5 --> system is in remove students from class mode
		// mode = 6 --> this is the view courses mode
		//mode = 7 --> this is for the removing course from class mode
		//mode = 8 --> mannual add student to course
		//mode = 9 -->this is the add course mode
		//mdoe = 10 --> this is the remove course from semeter mode
		//mode  = 11 --> this is for edit teacher courses
		//mode = 12 --> this is the remove course ffrom teacher mode
		editMode = mode;
	}
	
	public int getEditMode(){
		return editMode;
	}
	
	public int checkClassRegex(String className){
		return 	ccr.checkClassRegex(className);
	}
	
	public void planNextSemester(){
		ss.planNextSemester();
	}
	
	public void editCurrentSemester(){
		ss.editCurrentSemester();
	}
	
	public void viewExisitingClasses(){
		ss.viewExistingClasses();
	}
	
	public void createNewClass(){
		ss.createNewClass();
	}
	
	public void editExistingClass(){
		ss.editExistingClass();
	}
	
	public void upperTable(Point point, int semester, int classToEdit){
		ss.upperTable(point, semester, classToEdit);
	}
	
	public void getAllOtherStudents(int classToEdit){
		ss.getAllOtherStudents(classToEdit);
	}
	
	public void addStudentToClass(int studentID, int classID, int semester, Point point){
		ss.addStudentToClass(studentID, classID, semester, point);
	}
	
	public void removeStudentsFromClass(int classID){
		ss.removeStudentsFromClass(secretaryConnection, crs);
		
	}
	
	public void handleLowerTableEvents(Point point, int semester){
		ss.handleLowerTableEvents(point, semester);
	}
	
	public void viewCourses(){
		ss.viewCourses();
	}
	
	public void showClassCourses(Point point){
		ss.showClassCourses(point, secretaryConnection, crs);
	}
	
	public void setLastAssigningErrorLog(String logName){
		assigningErrorLogName = logName;
	}
	
	public String getLastAssigningErrorLog( ){
		return assigningErrorLogName;
	}
	
	public void viewAssiginingErroLOg(){
		ss.viewAssiginingErrorLog(secretaryConnection, crs);
	}
	
	public void assignStudentToCourse(String tableName, String courseID, int semesterReal, int classID, int automatic, int semesterID){
		ss.assignStudentToCourse(tableName, courseID, semesterReal, classID, automatic, semesterID);
	}
	
	public void viewAssignedClasses(){
			ss.viewAssignedClasses();
	}
	
	public void viewStudents(){
		ss.viewStudents();
	}
	
	public void setStudentID(int id){
		studentID = id;
	}
	
	public void setTeacherID(double id){
		teacherID = id;
	}
	
	public int getStudentID(){
		return studentID;
	}
	
	public double getTeacherID(){
		return teacherID;
	}
	
	public void setSource(int source, int dest){
		// 0 - yellow, no changes
		// 1 - green, this is the source (where you should pick)
		// 2 - red, this is the destination, where the data goes
		sourceTable = source;
		destinationTable = dest;
	}
	
	public int getSource(){
		return sourceTable;
	}
	
	public int getDestination(){
		return destinationTable;
	}
	
	public void openMail(int userID, createMailBox cmb){
		cmb.setConnection(secretaryConnection);
		cmb.setJDBC(crs);
		cmb.setUserID(userID);
		cmb.setSemesterID(ds.identify(ssc.getSemesterForEdit(), secretaryConnection, crs));
		ss.openMail(userID, cmb);
	}
	
	public int getMyID(){
		return myID;
	}
	
	public void viewTeachers(){
		ss.viewTeachers(secretaryConnection, crs);
	}
}
